﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Besture.Bll;
using Besture.Model;
using Besture.Common.Definition;
using Besture.Utils;
using System.Data;

namespace Besture.UI.Service
{
    public partial class Productsdetail : System.Web.UI.Page
    {
        //Banner图
        public ImageModel _BannerTop { get; set; }
        public ProductModel _News { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session[Constants.SESSION_KEY_LOGIN_USER] == null&&!string.IsNullOrEmpty(Request["buy"]))
                {
                    Fun.Alert("请先登录！", "/Land/Land.aspx");
                    return;
                }
                if (Request.QueryString["id"] != null && !string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    InitData(Convert.ToInt32(Request.QueryString["id"]));
                }
            }
            GetBanner();
        }

        #region 初始化数据
        private void InitData(int id)
        {
            hidPID.Value =id+"";
            var model = new ProductDac().GetModel(id);
            if (model != null)
            {
                _News = model;
                if (model.ProductState == Constants.DICTIONARY_Code_StateNew)
                {
                    if (Session[Constants.SESSION_KEY_LOGIN_USER] != null)
                    {
                        btnSubmitBuy.Visible = true;
                    }
                    else
                    {
                        btnSubmitBuy.Visible = false;
                    }
                }
                else
                {
                    btnSubmitBuy.Visible = false;
                }
            }
            else
                _News = new ProductModel();
        }

        #endregion

        #region 获取Banner
        /// <summary>
        /// 获取Banner
        /// </summary>
        public void GetBanner()
        {
            ImageDac theDac = new ImageDac();
            DataTable dt = theDac.GetDataTable(Constants.DICTIONARY_TYPE_AboutServiceClass);

            rpFinancialList.DataSource = dt;
            rpFinancialList.DataBind();


            ImageDac imageDac = new ImageDac();
            ImageModel imgModel = imageDac.GetDataTableByCode(Constants.DICTIONARY_CODE_BannerService, Constants.DICTIONARY_TYPE_IMAGES);
            if (imgModel != null)
                _BannerTop = imgModel;
            else
                _BannerTop = new ImageModel();
        }
        #endregion

        protected void btnSubmitBuy_Click(object sender, EventArgs e)
        {
            string msg = "报名成功";
            object user = Session[Constants.SESSION_KEY_LOGIN_USER];
            if (user != null)
            {
                UserProductModel model = new UserProductModel();
                UserProductDac bll = new UserProductDac();
                model.PID = Convert.ToInt32(hidPID.Value);
                model.UID = (user as UserModel).ID;
                model.IsDeleted = 0;
                model.BuyDate = DateTime.Now;
                model.CreateTime = DateTime.Now;
                model.State = 0;

                msg = bll.SubmitActive(model, (user as UserModel).Phone);
                InitData(model.PID.Value);
            }
            else
            {
                msg = "请先登录后再报名";
            }
            Fun.Alert(this.Page,msg);
        }
    }
}